import 'package:flutter/material.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter_juwo/ui/pages/commonProblem/qa_entity.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';

import 'dart:convert';
import 'QAPage.dart';

class CommonProblemPage extends StatefulWidget {
  @override
  _CommonProblemPageState createState() => _CommonProblemPageState();
}

class _CommonProblemPageState extends State<CommonProblemPage>
    with SingleTickerProviderStateMixin {
  ScrollController _scrollController;
  TabController _tabController;
  QaEntity datas;

  Future readJson() async {
    var response =
        await DefaultAssetBundle.of(context).loadString("assets/question.json");
    print("response:${response}");

    var data = json.decode(response.toString());

    QaEntity qaEntity = QaEntity.fromJson(data);

    print('qaEntity:${qaEntity.qajson.length}');
    setState(() {
      datas = qaEntity;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController = ScrollController();
    _tabController = TabController(length: 5, vsync: this);
    readJson();
  }

  @override
  Widget build(BuildContext context) {
    if (datas != null) {
      List<Widget> widgets=[];
      for(int i=0;i<datas.qajson.length;i++){
        widgets.add(QAPage(qaQajson:datas.qajson[i]));
      }

      return Scaffold(
        appBar: this.getAppBar(datas),
        body: TabBarView(controller: _tabController, children: widgets),
      );
    } else {
      return Scaffold(
        appBar: UiUtils.appBar(context, "错误"),
        body: Center(
          child: Text("加载数据失败！"),
        ),
      );
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _scrollController.dispose();
    _tabController.dispose();
    super.dispose();
  }

  getAppBar(QaEntity datas) {
    return AppBar(
      brightness: Brightness.light,
      backgroundColor: Colours.white,
      title: Text(
        '常见问题',
        style: TextStyle(
            fontSize: ScreenUtil().setSp(34),
            fontFamily: Font.PingFangBlod,
            color: Colours.appC1),
      ),
      centerTitle: true,
      elevation: 0.0,
      leading: InkWell(
          highlightColor: Color(0xFFB3E5FC),
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            padding: EdgeInsets.fromLTRB(12.0, 0.0, 12.0, 0.0),
            child: Image.asset(
              Utils.getImgPath('return_left'),
              width: 18.0,
              height: 20.0,
            ),
          )),
      bottom: TabBar(
          //isScrollable: true,
          controller: _tabController,
          indicatorColor: Color(0xFFFFFFFF),
          indicatorWeight: ScreenUtil().setWidth(1),
          indicatorPadding: EdgeInsets.fromLTRB(8, 0, 8, 0),
          labelPadding: EdgeInsets.only(bottom: 0),
          unselectedLabelColor: Colours.appC1,
          labelColor: Color(0xFFE3393C),
          tabs: <Widget>[
            Tab(text: datas.qajson[0].name, icon: Icon(Icons.desktop_mac)),
            Tab(text: datas.qajson[1].name, icon: Icon(Icons.phone_android)),
            Tab(text: datas.qajson[2].name, icon: Icon(Icons.cached)),
            Tab(text: datas.qajson[3].name, icon: Icon(Icons.spellcheck)),
            Tab(text: datas.qajson[4].name, icon: Icon(Icons.attach_money)),
          ]),
    );
  }
}
